<template>
    <view>
        <UserInfo :userId="userId"/>
        <uni-card>
            <uni-segmented-control :current="current" activeColor="#e30e33" styleType="text" :values="['笔记', '收藏']" @clickItem="onClickItem" />
        </uni-card>
        <view class="content">
            <view v-show="current === 0">
                <NoteCard :list="list[0].rows" />
            </view>
            <view v-show="current === 1">
                <NoteCard :list="list[1].rows" />
            </view>
            <view v-show="current === 2">
                <NoteCard :list="list[2].rows" />
            </view>
        </view>

        <uni-load-more iconType="circle" :status="status" />

    </view>
</template>
  
<script>
import NoteCard from '@/components/NoteCard'
import UserInfo from '@/components/UserInfo'
import {
    httpUserNote,
    httpUserFavorite,
    httpUserLike
} from '@/utils/api.js'
export default {
    components: { NoteCard, UserInfo },
    data() {
        return {
            showLeft: false,
            current: 0,
            userId: '',
            user: {
                id: '',
                phone: '',
                name: '',
                avatar: '',
                brief: '',
                fans: 0,
                focus: 0,
                like: 0,
                favorite: 0
            },
            noteList: [],
            favoriteList: [],
            likeList: [],
            list: [
                {
                    rows: [],
                    page: 1
                },
                {
                    rows: [],
                    page: 1
                },
                {
                    rows: [],
                    page: 1
                }
            ],
            size: 6,
            status: 'more',
        }
    },
    onLoad(options) {
        this.userId = options.userId
        this.getNotes()
    },
    // 下拉刷新
    async onPullDownRefresh() {
        await this.getNotes()
        uni.stopPullDownRefresh()
    },
    methods: {
        // 分段器
        onClickItem(e) {
            this.current = e.currentIndex
            this.getNotes(this.current)
            this.list.forEach(item => {
                // 重置
                item.rows = []
                item.page = 1
            })
        },
        // 获取笔记
        async getNotes(index = 0) {
            let http
            switch (index) {
                case 1:
                    http = httpUserFavorite
                    break;
                case 2:
                    http = httpUserLike
                    break;
                default:
                    http = httpUserNote
            }
            const { code, data } = await http(this.userId, {
                size: this.size,
                page: this.list[index].page,
            })
            if (code === 200) {
                const notes = data.rows
                if (notes.length === 0) {
                    this.status = 'noMore'
                    return false
                }
                this.list[index].rows = [...this.list[index].rows, ...notes]
                // this.list[index].page++
                this.status = 'more'
                if (notes.length < this.size) this.status = 'noMore'
            }
        },
        goto(url) {
            uni.navigateTo({
                url
            })
        },
    }
};
</script>
  
<style lang="scss"></style>